package org.song.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.song.annotation.PowerEquals;
import org.song.exception.result.AjaxResult;
import org.song.model.dto.TagDto;
import org.song.model.vo.Tag;
import org.song.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 博客标签控制层
 */
@Api(tags = "博客标签控制层")
@Controller
@RequestMapping(value = "/tag")
public class TagController {

    @Autowired( required = true )
    @Qualifier( value = "tagServiceImpl")
    private TagService tagService;

    /**
     * 查询所有的博客标签
     * @param tagDto
     * @return
     */
    @ApiOperation(value = "查询所有的博客标签" , notes = "查询所有的博客标签")
    @ResponseBody
    @RequestMapping(value = "/listTags" , method = RequestMethod.GET)
    public AjaxResult<PageInfo<Tag>> listTags(@ApiParam(value = "tagDto实例接受查询参数") @NonNull TagDto tagDto) {
        PageHelper.startPage(tagDto.getPage() , tagDto.getSize());
       return AjaxResult.success(new PageInfo<>(tagService.listTags(tagDto)));
    }

    /**
     * 删除博客标签
     * @param tag
     * @return
     */
    @PowerEquals
    @ApiOperation(value = "删除博客标签" , notes = "删除博客标签")
    @ResponseBody
    @RequestMapping(value = "/deleteTag" , method = RequestMethod.DELETE)
    public AjaxResult<Integer> deleteTag(@ApiParam(value = "接受需要删除的博客标签") @NonNull Tag tag) {
        return AjaxResult.success(tagService.deleteTag(tag));
    }

    /**
     * 增加博客标签
     * @param tag
     * @return
     */
    @PowerEquals
    @ApiOperation(value = "增加博客标签" , notes = "增加博客标签")
    @ResponseBody
    @RequestMapping(value = "/insertTag" , method = RequestMethod.PUT)
    public AjaxResult<Integer> insertTag(@ApiParam(value = "博客标签") @NonNull @RequestBody Tag tag) {
        return AjaxResult.success(tagService.insertTag(tag));
    }

    /**
     * 更新博客标签
     * @param tag
     * @return
     */
    @PowerEquals
    @ApiOperation(value = "更新博客标签" , notes = "更新博客标签")
    @ResponseBody
    @RequestMapping(value = "/updateTag" , method = RequestMethod.POST)
    public AjaxResult<Integer> updateTag(@ApiParam(value = "根据传入的标签信息进行博客标签更新") @NonNull @RequestBody Tag tag) {
        return AjaxResult.success(tagService.updateTag(tag));
    }


}
